/**
 * TManTest.java
 * ************************** 
 * @date May 11, 2011
 * @author Michele Tamburini
 * 
 * **************************
 */
package JUnitTests;

import static org.junit.Assert.fail;
import gossipServices.basic.nodeDescriptors.GeneralNodeDescriptor;
import gossipServices.basic.nodeDescriptors.NodeDescriptor;
import gossipServices.basic.view.ConcretePartialView;
import gossipServices.basic.view.PartialView;
import gossipServices.basic.view.PartialView.PEER_SELECTION_ENUM;
import gossipServices.bootstraping.tman.TMan;
import gossipServices.bootstraping.tman.TManNetworkManager;
import gossipServices.bootstraping.tman.TManRankingMethod;
import gossipServices.bootstraping.tman.rankingMethods.SortedRing;
import gossipServices.pss.ConcretePeerSamplingService;
import gossipServices.pss.PeerSamplingServiceAPI;

import java.util.Vector;

import networkRmi.NetworkManager;

import org.junit.Before;
import org.junit.Test;

/**
 * Description: 
 *
 */
public class TManTest {
	
	TMan tman;
	NetworkManager nm = new FakeNetMan();
	PartialView tManView;
	PartialView initView;
	PeerSamplingServiceAPI pss;
	NodeDescriptor localNode;
	int targetViewSize = 5;
	int pssInitializationViewSize = 4;
	int tabuListSize = targetViewSize;
	TManRankingMethod rankingMethod = new SortedRing(targetViewSize);
	String subcloudID = "newSubCloud";
	
	int NETWORK_NODES = 10;
	String networkNodes[] = new String[NETWORK_NODES];
	Vector<NodeDescriptor> initialVec = new Vector<NodeDescriptor>(NETWORK_NODES);
	PartialView view;

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		for(int i=0; i<NETWORK_NODES; i++){
			networkNodes[i] = "node"+i;
			initialVec.add(new GeneralNodeDescriptor(networkNodes[i]));
		}
		view = new ConcretePartialView(initialVec);
		
		tManView = new ConcretePartialView(0);
		pss = new ConcretePeerSamplingService(1, 1, true, true, PEER_SELECTION_ENUM.rand, 
				view, nm);
//		initialzeInitViewFromPSS();
		localNode = new GeneralNodeDescriptor("node2###");
		
		initializeInitViewFromTheRankingMethod();
		print("initView is:\n"+initView);
		
		tman = new TMan(20, initView, pss, new TManNetworkManager(nm, subcloudID), 
				localNode, rankingMethod, 
				targetViewSize, 1, tabuListSize, subcloudID);
		
	}

	/**
	 * 
	 */
	private void initialzeInitViewFromPSS() {
		Vector<NodeDescriptor> tempVec = new Vector<NodeDescriptor>();
		NodeDescriptor node;
		print("View initialization from the PSS");
		for(int i=0; i < pssInitializationViewSize; i++){
			node = pss.getPeer();
			while(tempVec.contains(node))
				node = pss.getPeer();
			tempVec.add(node);
		}
		initView =new ConcretePartialView(tempVec);
		
	}
	
	private void initializeInitViewFromTheRankingMethod(){
		print("View initialization from the RankingMethod: "+
				rankingMethod.getClass().getSimpleName());
		initView = rankingMethod.rank(localNode, view);
	}
	
	private void print(Object o){
		System.out.println(o);
	}

	/**
	 * Test method for {@link gossipServices.bootstraping.tman.TMan#TMan(long, gossipServices.basic.view.PartialView, gossipServices.basic.view.PartialView, gossipServices.pss.PeerSamplingServiceAPI, gossipServices.bootstraping.tman.TManNetworkManager, gossipServices.basic.nodeDescriptors.NodeDescriptor, gossipServices.bootstraping.tman.TManRankingMethod, int, int)}.
	 */
	@Test
	public void testTMan() {
		print("The total Network is:\n"+view);
		
	}

	/**
	 * Test method for {@link gossipServices.bootstraping.tman.TMan#getOverlayNeighbors()}.
	 */
	@Test
	public void testGetOverlayNeighbors() {
		tman.start();
		print("TMan initialization is:\n"+tman.getOverlayNeighbors());
		try {
			Thread.sleep(4000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		fail("Not yet implemented");
	}

	/**
	 * Test method for {@link gossipServices.bootstraping.tman.TMan#getServiceClass()}.
	 */
	@Test
	public void testGetServiceClass() {
		fail("Not yet implemented");
	}

}
